#ifdef _CH_
#pragma package <opencv>
#endif


#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "sobel.h"

#include <iostream> 
using namespace std;

int thresh = 50;
IplImage *img = 0;
IplImage *imgx = 0;
IplImage *imgy = 0;
IplImage *imgxy = 0;
IplImage *imgx0 = 0;
IplImage *imgy0 = 0;
const char *wndname_orig = "My Sobel original";
const char *wndnamex = "My Sobel x-derivative";
const char *wndnamey = "My Sobel y derivative";
const char *wndnamexy = "My Sobel xy derivative";
 
int main (int argc, char **argv)
{
  int i, c;


  if (argc < 2) {
    printf ("Usage: %s <filename>/n", argv[0]);
    exit(1);
  }

  // Lê arquivo de entrada. Imagens são convertidas para Grace Kelly.
  img = cvLoadImage (argv[1], CV_LOAD_IMAGE_GRAYSCALE);

  if (!img) {
    printf ("Couldn't load %s\n", argv[1]);
    exit(2);
  }
  


  // Cria imagens do mesmo tamanho q a entrada, double, um canal.
  CvSize img_size = cvGetSize(img);

  imgx0 = cvCreateImage(img_size, IPL_DEPTH_32F, 1);
  imgy0 = cvCreateImage(img_size, IPL_DEPTH_32F, 1);
  imgx = cvCreateImage(img_size, IPL_DEPTH_8U, 1);
  imgy = cvCreateImage(img_size, IPL_DEPTH_8U, 1);

  // Copia (converte formato e arruma escala...)
  //cvConvertScale(img, imgx, 1/256.0, 0);
  //cvConvertScale(img, imgy, 1/256.0, 0);
  cvConvertScale(img, imgx0, 1/256.0, 0);
  cvConvertScale(img, imgy0, 1/256.0, 0);


  // Chama o 'seu' Sobel pra trabalhar.
  cvSobel (imgx0, imgx0, 1, 0, 3);
  cvSobel (imgy0, imgy0, 0, 1, 3);


  // Calcula extremos
  double minx, miny;
  double maxx, maxy;
  cvMinMaxLoc( imgx0, &minx, &maxx);
  cvMinMaxLoc( imgy0, &miny, &maxy);

  cout << minx << ' '
       << miny << ' '
       << maxx << ' '
       << maxy << endl;
    

  // Arruma as escalas, pq os valores devem ser entre 0.0 e 1.0 na
  // hora de mostrar na tela. Pra isso, calculamos uma mesma escala a
  // ser aplicada às duas imagens.

  double sc = max(abs(maxx),max(abs(maxy),max(abs(minx),abs(miny))));
  sc=1.0/sc;
  printf("%f\n",sc);

  char nomex[13];
  char nomey[13];
 
   for ( i = 0 ; i <= 100; i+=1) {
    // O rigoroso seria isso
    //cvScale(imgx, imgx, sc,  0.5);
    //cvScale(imgy, imgy, sc,  0.5);
    //// Mas podemos dar uma exagerada
    cvScale(imgx0, imgx, i*4*sc,  128);
    cvScale(imgy0, imgy, i*4*sc,  128);
    
    sprintf(nomex, "imgx%04d.png", i);
    sprintf(nomey, "imgy%04d.png", i);

    //printf("%s %s\n", nomex, nomey);
    
    get_properties(imgx,wndnamex);
    get_properties(imgy,wndnamey);

    cvSaveImage(nomex,imgx);
    cvSaveImage(nomey,imgy);
    /// Parte GUI
    cvNamedWindow (wndname_orig, 2);
    cvNamedWindow (wndnamex, 2);
    cvNamedWindow (wndnamey, 2);
    
    cvMoveWindow (wndname_orig, 320 , 0);
    cvMoveWindow (wndnamey, 640, 0);
    
    
    cvShowImage (wndname_orig, img);
    cvShowImage (wndnamex, imgy);
    cvShowImage (wndnamey, imgx);
    
    c = cvWaitKey(0);
     
   }
  cvReleaseImage (&img);
  cvReleaseImage (&imgx);
  cvReleaseImage (&imgy);
  
  cvDestroyAllWindows ();
  return 0;

}
